<template>
  <div class="table-box">
    <div class="table-search">
      <el-form ref="formRef" :inline="true" :model="form">
        <el-form-item label="管理员账号">
          <el-input v-model="form.username" placeholder="管理员账号" />
        </el-form-item>
        <el-form-item label="用户UID">
          <el-input v-model="form.event_uid" placeholder="用户UID" />
        </el-form-item>
        <el-form-item label="操作类型">
          <el-select v-model="form.event_type" placeholder="操作类型">
            <el-option v-for="item in eventTypeList" :key="item.event_type" :label="item.event_value" :value="item.event_type"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间">
          <el-date-picker v-model="form.timeData" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="截止日期" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border ref="tableRef" v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="username" label="操作员账号" align="center" />
          <el-table-column prop="event_uid" label="用户UID" align="center" />
          <el-table-column prop="event_type" label="操作类型" align="center" />
          <el-table-column prop="event_table" label="操作表" align="center" />
          <el-table-column prop="desc" label="备注" align="center" />
          <el-table-column prop="results" label="结果备注" align="center" />
          <el-table-column prop="create_time" label="时间" align="center" :formatter="formatterTime"  width="180"/>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue';
import { exportToExcel,formatterTime,timestamp } from '@/utils/utils';
import { logsTypeApi, logsApi } from '@/api/system/system';

const tableRef = ref(null);
const formRef = ref();
const tableData = ref([]);
const eventTypeList = ref([]);
const formData = {
  username: '',
  event_type: '',
  timeData: [],
  event_uid: '',
};
const form = reactive({ ...formData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    username: form.username,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : '',
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : '',
    event_type: form.event_type,
    event_uid: form.event_uid,
  };
};

const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

const loading = ref(false);

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await logsApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  // const { event_uid } = param();
  // if (!event_uid) {
  //   ElMessage({
  //     message: "请输入用户UID",
  //     type: "warning",
  //   });
  //   return;
  // }
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await logsApi(data);

  let head = {
    id: 'ID',
    username: '管理员账号',
    event_uid: '用户UID',
    event_type: '操作类型',
    event_table: '操作表',
    desc: '备注',
    results: '结果备注',
    create_time: '时间戳',
  };
  exportToExcel(res.data.results, head, '操作日志');
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(async () => {
  try {
    getTbaleData();
    const eventTypeRes = await logsTypeApi();
   
    eventTypeList.value = eventTypeRes.data;
  } catch (error) {
    console.log(error);
  }
});
</script>
